﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Skynet.CommonLibrary.ThreadRefreshTime
{
    /// <summary>
    /// 读取XML配置时类型转换扩展
    /// </summary>
    ///<remarks>
    /// 作者：         李志强
    /// 开发时间：  2011-03-28
    /// </remarks>
   public  static   class XMLExtension
    {
       public static void SetTypeValue(this PropertyInfo info, object obj, string fieldValue)
       {
           if (info == null && !info.CanWrite)
               return;

           if (IsType(info.PropertyType, "System.String"))
           {
               info.SetValue(obj, fieldValue,null);
           }

           if (IsType(info.PropertyType, "System.Int32"))
           {
               if (string.IsNullOrEmpty(fieldValue))
               {
                   info.SetValue(obj, 0,null);
               }
               else
               {
                   info.SetValue(obj, Int32.Parse(fieldValue), null);
               }
           }

           if (IsType(info.PropertyType, "System.DateTime"))
           {
               if (string.IsNullOrEmpty(fieldValue))
               {
                  
               }
               else
               {
                   DateTime dt;
                   DateTime.TryParse(fieldValue, out  dt);
                   info.SetValue(obj, dt, null);
               }
           }
       }

         public static bool IsType(Type type, string typeName)
        {
             if (type.ToString() == typeName)
                 return true;
             if (type.ToString() == "System.Object")
                 return false;
 
             return IsType(type.BaseType, typeName);
         }

    }
}
